#!/usr/bin/env python
# -*- coding: utf-8 -*-

' 加解密 '

from Crypto.Cipher import AES
import base64


class Prpcrypt(object):
    def __init__(self):
        self.key   = 'jltooooooooooooo'
        self.mode  = AES.MODE_CBC
        self.iv    = 'jltooooooooooooo'
        self.BS    = AES.block_size
        self.pad   = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
        self.unpad = lambda s: s[0:-ord(s[-1])]
        self.ciphertext = ''

    #   加密
    def encrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.iv)
        self.ciphertext = cryptor.encrypt(self.pad(text))

        return self.ciphertext.encode('hex')

    #   解密
    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.iv)
        plain_text = self.unpad(cryptor.decrypt(text.decode('hex')))

        return plain_text




if __name__ == '__main__':
    aes = Prpcrypt()
    my_str = '   123000'
    print 'before:', my_str
    print 'encrypt:', aes.encrypt(my_str)
    print 'decrypt:', aes.decrypt(aes.encrypt(my_str))